package com.study.stack;

/**
 * - *@ClassName Calculator
 * - *@Author LiuHuaBing
 * - *@Date 2021/11/30 13:57
 * - *@Version 1.0
 * - 栈模拟计算器
 */
public class Calculator {

    public int calculate(String expression) {
        ArrayStack numStack = new ArrayStack(100);
        ArrayStack symbolStack = new ArrayStack(100);

        for (int i = 0; i < expression.length(); i++) {
            String k = String.valueOf(expression.toCharArray()[i]);
            if (!"+-*/".contains(k)) {
                numStack.push(Integer.parseInt(k));
            } else {
                if (symbolStack.isNull()) {
                    symbolStack.push(k);
                } else {
                    String symbol1 = (String) symbolStack.pop();
                    int newNum;
                    if (!("+-".contains(symbol1) && !"+-".contains(k))){
                        //while (!("+-".contains(symbol1) && !"+-".contains(k))) {
                            int number1 = Integer.parseInt( numStack.pop().toString());
                            int number2 = Integer.parseInt(numStack.pop().toString());
                            newNum = this.doc(number1,number2,symbol1);
                            numStack.push(newNum);
                            //symbol1 = (String) symbolStack.pop();
//                            if (symbol1 ==null){
//                                break;
//                            }
                       // }
//                        if (symbol1!=null){
//                            symbolStack.push(symbol1);
//                        }
                        symbolStack.push(k);
                    }else {
                        symbolStack.push(symbol1);
                        symbolStack.push(k);
                    }
                }
            }
        }

        while (!symbolStack.isNull()) {
            String symbol = (String) symbolStack.pop();
            Integer res = null;

            int number1 = Integer.parseInt(numStack.pop().toString());

            int number2 = Integer.parseInt(numStack.pop().toString());
//            switch (symbol) {
//                case "*":
//                    res = number2 * number1;
//                    break;
//                case "/":
//                    res = number2 / number1;
//                    break;
//                case "+":
//                    res = number2 + number1;
//                    break;
//                case "-":
//                    res = number2 - number1;
//                    break;
//            }
            res = this.doc(number1,number2,symbol);
            numStack.push(res);

        }
        return Integer.parseInt( numStack.pop().toString());
    }

    public static int doc(int number1 ,int number2,String symbol){

        switch (symbol) {
            case "*":
                number2 = number2 * number1;
                break;
            case "/":
                number2 = number2 / number1;
                break;
            case "+":
                number2 = number2 + number1;
                break;
            case "-":
                number2 = number2 - number1;
                break;
        }
        return number2;


    }

}
